package com.sign2024.sign2024project.goodsmaterials.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.goodsmaterials.WzPerformance;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzPerformanceMapper;
import com.sign2024.sign2024project.goodsmaterials.param.WzParam;
import com.sign2024.sign2024project.goodsmaterials.service.WzPerformanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/8/26 8:55
 */
@Service
public class WzPerformanceServiceImpl implements WzPerformanceService {

    @Resource
    private WzPerformanceMapper wzPerformanceMapper;

    @Override
    public void insertBatchWzPerformance(List<WzPerformance> list, Integer taskId) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "任务id为空");
        } else {
            // 批量新增
            wzPerformanceMapper.insertBatchWzPerformance(list, taskId);
        }
    }

    @Override
    public void deleteByTaskId(Integer taskId) {
        wzPerformanceMapper.deleteByTaskId(taskId);
    }

    @Override
    public Page<WzPerformance> pageWzPerformance(WzParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<WzPerformance> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<WzPerformance> list = wzPerformanceMapper.selectWzPerformanceByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = wzPerformanceMapper.pageWzPerformanceCount(param);
        page.setTotal(aLong);
        page.setRecords(list);

        return page;
    }
}
